package com.pulsarwebdevelopment.concordance.plugins.android;

import java.io.StringWriter;

import com.pulsarwebdevelopment.concordance.R;
import com.pulsarwebdevelopment.concordance.Concordance;
import com.pulsarwebdevelopment.concordance.ConcordanceOutput;
import com.pulsarwebdevelopment.concordance.ConcordanceOutputStream;
import com.pulsarwebdevelopment.concordance.Tools;
import com.pulsarwebdevelopment.concordance.Verse;
import com.pulsarwebdevelopment.concordance.plugins.SearchManager;

import android.app.Activity;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.widget.EditText;

public class LiveSearchActivity extends Activity {
	public static final String CONC_ID_KEY_NAME = "concordanceId";

	private Handler mHandler = new Handler();

	private SearchManager searchMan = new SearchManager();

	protected Concordance conc;
	protected WebView results;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.live_search_screen);

		Bundle extras = getIntent().getExtras();
		int concId = extras.getInt(CONC_ID_KEY_NAME);
		
		conc = new Concordance(concId, new ContextWrapper(this.getBaseContext()).getFilesDir());

		final EditText inputBox = (EditText) findViewById(R.id.liveSearchSearchText);
		results = (WebView) findViewById(R.id.liveSearchSearchResults);

		Runnable watcherAndSearcher = new Runnable() {
			public void run() {
				String newSearchText = inputBox.getText().toString();

				//if there is not a current search, and the search string has changes, run a a new search
				if(!searchMan.isSearchRunning() && !newSearchText.equals(searchMan.getLastStringSearched())){
					//results.loadData("hello '" + newSearchText + "'", "text/html", "utf8");

					searchMan.setSearchIsRunning(true);

					synchronized (results) {
						results.loadData(getOccurences(newSearchText), "text/html", "utf8");
					}
					
					searchMan.setSearchIsRunning(false);

					searchMan.setLastStringSearched(newSearchText);
				}

				//put another check on the queue
				mHandler.postDelayed(this, 1000);
			}
		};

		//put initial check in the queue  
		mHandler.postDelayed(watcherAndSearcher, 1000);


	}

	private String getOccurences(String pattern){
		//set up the output streams
		StringWriter output = new StringWriter();
		ConcordanceOutput<String, Verse> outputStream = 
			new ConcordanceOutputStream<String, Verse>(output, ConcordanceOutputStream.ListStyle.SINGLE_LINE_TRUNCATED);

		//determine how many search strings were included 
		int lastIndexOfSpace = pattern.lastIndexOf(' ');
		if(lastIndexOfSpace < 1 || lastIndexOfSpace == (pattern.length() -1)){
			conc.getMatchingWordSet(Tools.capitalize(pattern.trim()), outputStream);
			
		}else{
			String[] patterns = pattern.split(" ");
			String[] pattList = new String[patterns.length];
			for(int i = 0; i < patterns.length; i++){
				pattList[i] = Tools.capitalize(patterns[i]);
			}
			//System.out.println("Patterns: " + Arrays.toString(pattList));
			conc.getMultipleMatchingWordSet(outputStream, pattList);

		}
		
		return output.toString();
	}
}
